package server

import (
	"vmapp/app0/internal/conf"
	"vmapp/app0/internal/data/base"
	"vmapp/app0/internal/service"
	"vmapp/pkg/middleware"

	"github.com/gin-gonic/gin"
)

func NewGin(
	c *conf.AppConf,
	bc *conf.BootComponent,
	data *base.Data,
	sysService *service.SysService,
	userService *service.UserService,
	roleService *service.RoleService,
	fileService *service.FileService,
	logService *service.LogService,
	menuService *service.MenuService,
) *gin.Engine {
	r := gin.New()
	r.Use(middleware.NewRecoverLog(bc.Logger))
	r.Use(middleware.NewRequestLog(bc.Logger))
	// 公共API
	api := r.Group("/api")
	api.POST("/login", sysService.Login)
	api.POST("/register", sysService.Register)
	api.POST("/forget", sysService.Forget)
	api.POST("/getcode", sysService.GetCode)
	api.POST("/captcha", sysService.Captcha)
	// 系统API
	sys := api.Group("/sys")
	sys.POST("/logout", sysService.LogOut)
	sys.POST("/refertoken", sysService.ReferToken)
	sys.POST("/restart", sysService.Restart)
	sys.POST("/update_userpass", sysService.UpdateUserPass)
	sys.POST("/update_userprofile", sysService.UpdateUserProfile)

	// 用户管理
	user := api.Group("/user")
	user.GET("/list", userService.ListUser)
	user.POST("/create", userService.CreateUser)
	user.POST("/update", userService.UpdateUser)
	user.POST("/delete", userService.DeleteUser)
	user.POST("/repassword", userService.RepassUser)
	user.POST("/resetoken", userService.ResetToken)
	user.GET("/info", userService.GetUserInfo)
	user.GET("/get", userService.GetUser)

	// 角色管理
	role := api.Group("/role")
	role.GET("/list", roleService.ListRole)
	role.POST("/create", roleService.CreateRole)
	role.POST("/update", roleService.UpdateRole)
	role.POST("/delete", roleService.DeleteRole)
	role.POST("/get", roleService.GetRole)
	role.GET("/info", roleService.GetUserRole)

	// 文件管理
	file := api.Group("/file")
	file.GET("/list", fileService.ListFile)
	file.POST("/upload", fileService.UploadFile)
	file.POST("/delete", fileService.DeleteFile)

	// 日志管理
	log := api.Group("/log")
	log.GET("/event/list", logService.ListEventLog)
	log.GET("/send/list", logService.ListSendLog)

	// 菜单管理
	menu := api.Group("/menu")
	menu.GET("/list", menuService.ListMenu)
	menu.GET("/tree", menuService.ListMenuTree)
	menu.POST("/create", menuService.CreateMenu)
	menu.POST("/delete", menuService.DeleteMenu)
	menu.POST("/update", menuService.UpdateMenu)
	menu.POST("/get", menuService.GetMenu)

	return r
}
